package com.sto.stoapp.thread;

import android.content.Context;
import android.os.Handler;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import com.sto.stoapp.db.StoFilePreference;
import com.sto.stoapp.http.BaseMessage;
import com.sto.stoapp.http.HttpManager;
import com.sto.stoapp.http.URLActions;
import com.sto.stoapp.http.request.LoginRequest;
import com.sto.stoapp.http.response.LoginResponse;
import com.sto.stoapp.util.DateUtil;
import com.sto.stoapp.util.Helper;

import java.util.Date;

public class VerificationUpgradeThread extends BaseThread {
	public static int SELECTABLE_UPGRADE = 1;
	public static int FORCIBLE_UPGRADE = 2;
	public static int NO_UPGRADE = 3;
	public static int UPGRADE_ERROR = 4;
	Context context;
	Handler handler;
	LoginRequest request;

	public VerificationUpgradeThread(Context context, LoginRequest request,
			Handler handler) {
		super(context);
		this.context = context;
		this.handler = handler;
		this.request = request;
		showDialog();
	}

	@Override
	public void run() {
		super.run();

		HttpManager mgr = new HttpManager();
		Gson gs = new Gson();
		String str = mgr.doPostClient(request.buildToRequestString(),
				StoFilePreference.getInstance(context).getServerIp()
						+ URLActions.LOGIN_ACTION);
		LoginResponse response = null;
		try {
			response = gs.fromJson(str, new TypeToken<LoginResponse>() {
			}.getType());
		} catch (JsonSyntaxException e) {
			e.printStackTrace();
		}
		if (response != null
				&& response.respCode.equals(BaseMessage.REQUEST_SUCCESS)) {
			StoFilePreference.getInstance(context).setSessionId(
					response.sessionId);
			//这里判断是否要更新版本
			if (needUpgradByVersion(response.newSoftVersion)) {
				if (needUpgradByDate(response.updateDate)) {
					cancelDialog();
					handler.obtainMessage(FORCIBLE_UPGRADE, response)
							.sendToTarget();
				} else {
					cancelDialog();
					handler.obtainMessage(SELECTABLE_UPGRADE, response)
							.sendToTarget();

				}
			} else {
				cancelDialog();
				handler.obtainMessage(NO_UPGRADE).sendToTarget();
			}
		} else {
			cancelDialog();
			handler.obtainMessage(UPGRADE_ERROR,response).sendToTarget();
		}
	}

	/**
	 * 修改更新规则
     */
	private boolean needUpgradByVersion(String soft) {
		if (soft == null || soft.trim().equals(""))
			return false;
//		int nowVersion = Integer.parseInt(Helper.getVersionName(context)
//				.replace(".", ""));
//		int serverVersion = Integer.parseInt(soft.replace(".", ""));
//		return serverVersion > nowVersion;

		try{
			String version=Helper.getVersionName(context);
			String[] nowVersions= version.split("\\.");
			String[] serverVewsions=soft.split("\\.");

			int A0=Integer.valueOf(serverVewsions[0]);
			int a0=Integer.valueOf(nowVersions[0]);
			int A1=Integer.valueOf(serverVewsions[1]);
			int a1=Integer.valueOf(nowVersions[1]);
			int A2=Integer.valueOf(serverVewsions[2]);
			int a2=Integer.valueOf(nowVersions[2]);
			int A3=Integer.valueOf(serverVewsions[3]);
			int a3=Integer.valueOf(nowVersions[3]);

			if(A0>a0){
				return true;
			}
			if(A0<a0){
				return false;
			}
			if(A0==a0){
				if(A1>a1){
					return true;
				}else if(A1<a1){
					return false;
				}else if(A1==a1){
					if(A2>a2){
						return true;
					}else if(A2<a2){
						return false;
					}else {
						if(A3>a3){
							return true;
						}else{
							return false;
						}
					}
				}
			}
		}catch (Exception e){
			return false;
		}

		return false;

	}

	private boolean needUpgradByDate(String date) {
		if (date == null || date.trim().equals(""))
			return false;
		long serverDate = DateUtil.getLongTime(
				DateUtil.DEFAULT_DATE_TIME_FORMAT, date);
		long nowDate = new Date().getTime();
		return nowDate > serverDate;
	}
}
